/*
 * Copyright (c) 2011-2012 www.iflytek.com. All Rights Reserved.
 *  This software for customer relationship management system, developed by Noo team.
 *  Software code and design for the team, copy rights reserved.
 */
var editor;
var issue_add = function(){

    return {
        add_more: function()
        {
            var createobj_input=$('<input name="attachment" type="file"/>');
            var createobj_br=$('</br>');
            var createobj_del=$('<input type="button" value="{0}"/>'.format(language.common.remove));

            $('#file').append(createobj_br);
            $('#file').append(createobj_input);
            $('#file').append(createobj_del);

            createobj_del.click(function(){
                var  visitor_att_count=0;
                $("#file").find(":file").each(function()
                {
                    visitor_att_count+=1;
                })
                if(visitor_att_count<=1)
                {
                    createobj_input.attr("value","");
                    return ;
                }
                else{
                    createobj_input.remove();
                    createobj_br.remove();
                    createobj_del.remove();
                }
            })
            createobj_input.change(function(){
                if ($.browser.ie) {
                    // alert("IE无法计算文件大小")

                } else {
                    file_size = this.files[0].size;
                    console.log(file_size/1024/1024 + "MB");

                    var size = file_size / 1024;
                    if(size > 10240){
                        $.alert(language.issue.attachment_limit_msg,language.note.alert_title);
                        this.value="";
                        var num01 = file_size/1024;
                        num02 = num01.toFixed(2) ;
                    }else{
                        var num01 = file_size/1024;
                        num02 = num01.toFixed(2) ;
                    }
                }
                return true;
            })
        }

    }
}();
KindEditor.ready(function (K) {
    editor = K.create('#ta_qa_content', {
        themeType:'default',
        resizeType:1,
        urlType:'domain',
        tabIndex:2,
        width:540,
        shadowMode:false,
        allowPreviewEmoticons:false,
        allowImageUpload:true,
        allowFlashUpload:false,
        uploadJson:js_param.upload_url,
        afterCreate:function () {
            K.ctrl(this.edit.iframe.get(0).contentWindow.document, 13, function () {
                $("#ta_qa_content").parent().submit();
            });
        },
        afterChange:function () {
            this.sync();
        },
        items:['fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
            'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
            'insertunorderedlist', '|', 'image', 'link', '|', 'about']
    }) ,
    editor.html(language.issue.issue_tip_template.error)

});

// 接收反馈标题change事件 去除首位空格
function trim_summary(){
        var value = $("#txt_title").attr("value");
       $("#txt_title").attr("value",value.trim());
}

var check_stay= function(){
	var stay=false;
	return {
	form_submit	: function(data){
			switch(data){
			case 1:
				stay = true;
				break;
			case 2:
				stay = false;
				break;

			};
			$('#form_question').ajaxForm({
		        dataType:'json',
		        beforeSerialize:function ($form, options) {
		            editor.sync();
		        },
		        beforeSubmit:function () {
		            if (!$.isValid()) {
		                return false;
		            }

		            if (editor.html().length < 1) {
		                cdms.common.show_error('tr_form_msg', language.issue.issue_content_empty);
		                return false;
		            }

		            $('#btn_submit').attr('disabled', 'disabled');
		            cdms.common.show_info('tr_form_msg', language.issue.submit_wating_msg);
		        },
		        success:function (json) {
		            $('#btn_submit').removeAttr('disabled');
		                if (json.type) {
		                    $.alert({
		                        title:language.note.alert_title,
		                        message:json.msg,
		                        fnOk:function(){
		                        	if(stay){
		                        		location.reload();
		                        	}
		                        	else
		                            window.location.href="list";
		                        }
		                    });
		                } else {
		                    cdms.common.show_error('tr_form_msg', json.msg);
		                }
		        }
		    });
			$('#form_question').submit();
	    }
	}
}();

$(document).ready(function () {
    $('#new_issue_p_select').combobox({
        url:"new_issue_p_select" ,
        emptyText:language.issue.issue_project_select,
        onselect:function()
        {
            $("#p_id").attr("value",$('#new_issue_p_select').combobox("value"))
        }
    });
    $('#p_key_autocomp').autocomplete(js_param.project_load_url, {
        minChars:1,
        scroll:true,
        width:310,
        matchContains:true,
        autoFill:false,
        dataType:'json',
        extraParams:{format:'json'},
        parse:function (data) {
            var parsed = [];
            for (var i = 0; i < data.length; i++) {
                parsed[parsed.length] = {
                    data:data[i],
                    value:data[i],
                    result:data[i].name
                };
            }
            return parsed;
        },
        formatItem:function (data, i, n, v) {
            return  v.code +" "+ v.name;
        },
        formatResult:function (data, v) {
            return data.name;
        }
    }).result(function(event, item) {
            $("#p_id").attr("value",item.id);
            $("#p_key").attr("value",item.code);
        });
    var createobj_input=$('<input name="attachment" type="file"/>');
    var createobj_br=$('</br>');
    var createobj_del=$('<input type="button" value="{0}"/>'.format(language.common.remove));

    $('#file').append(createobj_br);
    $('#file').append(createobj_input);
    $('#file').append(createobj_del);

    createobj_del.click(function(){
        var  visitor_att_count=0;
        $("#file").find(":file").each(function()
        {
            visitor_att_count+=1;
        })
        if(visitor_att_count<=1)
        {
            createobj_input.attr("value","");
            return ;
        }
        else{
            createobj_input.remove();
            createobj_br.remove();
            createobj_del.remove();
        }
    })
    createobj_input.change(function(){
        if ($.browser.ie) {
            // IE无法计算文件大小

        } else {
            file_size = this.files[0].size;
            console.log(file_size/1024/1024 + "MB");

            var size = file_size / 1024;
            if(size > 10240){
            	$.alert(language.issue.attachment_limit_msg,language.note.alert_title);
                this.value="";
                var num01 = file_size/1024;
                num02 = num01.toFixed(2) ;
            }else{
                var num01 = file_size/1024;
                num02 = num01.toFixed(2) ;
            }
        }
        return true;
    })

        return true;



});
$("#radio_select").click(function(){
    if($('input[id=issue_type_1]:checked').size()!=0) {
        editor.html(language.issue.issue_tip_template.error);
       }
    if($('input[id=issue_type_2]:checked').size()!=0) {
        editor.html(language.issue.issue_tip_template.demand);
    }
    if($('input[id=issue_type_3]:checked').size()!=0) {
        editor.html(language.issue.issue_tip_template.proposal);
    }
    if($('input[id=issue_type_4]:checked').size()!=0){
        editor.html(language.issue.issue_tip_template.other);
    }
}) ;
